    //
//  ZoomingScrollView.m
//  coocsless
//
//  Created by fabian on 08.03.11.
//  Copyright 2011 roboboogie.us. All rights reserved.
//

#import "ZoomingScrollView.h"


@implementation ZoomingScrollView
@synthesize imageView;

-(id)initWithFrame:(CGRect)frame andImageName:(NSString *) pImageName {
	self = [super initWithFrame:frame];
    if (self) {
		UIImage *image = [UIImage imageNamed:pImageName];
		imageView = [[UIImageView alloc] initWithImage:image];
		[self addSubview:imageView];
		self.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
		
		self.maximumZoomScale = 3.0;
		self.minimumZoomScale = .5;
		self.clipsToBounds = YES;
		self.delegate = self;
    }
    return self;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {	
	return imageView;
}

-(void) layoutSubviews {
	[super layoutSubviews];
	CGSize boundsSize = self.bounds.size;
	CGRect frameToCenter = imageView.frame;
	
	// horizontale Zentrierung
	
	if(frameToCenter.size.width < boundsSize.width) 
		frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width)/2;
	else
		frameToCenter.origin.x = 0;
	
	if(frameToCenter.size.height < boundsSize.height)
		frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height)/2;
	else 
		frameToCenter.origin.y = 0;

	imageView.frame = frameToCenter;
}

- (void)dealloc {
	[imageView release];
    [super dealloc];
}


@end
